package com.linkage.educloud.ah.adapter.topic;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.bean.TopicChildBean;
import com.linkage.educloud.ah.util.ImageLoader;
import com.linkage.educloud.ah.view.activity.topic.TopicChildDetailActivity;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Mr'Tang on 2018/12/7.
 */

public class TopicMineAdapter extends XRecyclerView.Adapter<TopicMineAdapter.ViewHolder> {
    private List<TopicChildBean> mlist;
    private Context mcontext;
    private int mtype = 1;//1---我发布的 2---我回复的

    public TopicMineAdapter(Context context, List<TopicChildBean> datas, int type) {
        this.mcontext = context;
        this.mlist = datas;
        this.mtype = type;
    }

    public void addAll(List<TopicChildBean> data, boolean append) {
        if (mlist != null) {
            if (!append) {
                mlist.clear();
            }
            mlist.addAll(data);
        } else {
            mlist = data;
        }
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view;
        if (mtype == 1) {
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_topic_mine, viewGroup, false);
        } else {
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_topic_mine_replay, viewGroup, false);
        }
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
        final TopicChildBean bean = mlist.get(position);
        ImageLoader.loadAvatar(mcontext, bean.getFace(), viewHolder.item_topic_mine_avater);
        viewHolder.item_topic_mine_name.setText(bean.getNick());
        viewHolder.item_topic_mine_time.setText(bean.getPublish_time());
        viewHolder.item_topic_mine_context.setText(bean.getContent());
        viewHolder.item_topic_mine_label.setText(bean.getLabel());
        if (mtype == 1) {
            viewHolder.item_topic_mine_title.setText(bean.getName());
        } else {
            viewHolder.item_topic_mine_title.setText("回复：" + bean.getName());
        }
        viewHolder.item_topic_mine_comment_num.setText(String.valueOf(bean.getComment_num()));

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TopicChildDetailActivity.launch(mcontext, bean.getId());
            }
        });
    }

    @Override
    public int getItemCount() {
        return mlist.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        CircleImageView item_topic_mine_avater;
        TextView item_topic_mine_name;
        TextView item_topic_mine_time;
        TextView item_topic_mine_title;
        TextView item_topic_mine_context;
        TextView item_topic_mine_label;
        LinearLayout item_topic_mine_zan;
        TextView item_topic_mine_zan_num;
        TextView item_topic_mine_comment_num;

        public ViewHolder(View convertView) {
            super(convertView);
            item_topic_mine_avater = convertView.findViewById(R.id.item_topic_mine_avater);
            item_topic_mine_name = convertView.findViewById(R.id.item_topic_mine_name);
            item_topic_mine_time = convertView.findViewById(R.id.item_topic_mine_time);
            item_topic_mine_title = convertView.findViewById(R.id.item_topic_mine_title);
            item_topic_mine_context = convertView.findViewById(R.id.item_topic_mine_context);
            item_topic_mine_label = convertView.findViewById(R.id.item_topic_mine_label);
            item_topic_mine_comment_num = convertView.findViewById(R.id.item_topic_mine_comment_num);
            if (mtype == 2) {
                item_topic_mine_zan = convertView.findViewById(R.id.item_topic_mine_zan);
                item_topic_mine_zan_num = convertView.findViewById(R.id.item_topic_mine_zan_num);
            }
        }
    }
}